$(function () {
    var audit_datagrid = $("#audit_datagrid")
    var audit_dialog = $("#audit_dialog")
    var productItem_datagrid = $("#productItem_datagrid")
    var client_dialog = $("#client_dialog")
    var client_form = $("#client_form")
    client_dialog.dialog({
        width: 320,
        height: 420,
        top: 100,
        buttons: "#form_btns",
        closed: true,
        title: '投保人信息',
        onClose: function () {
            client_form.form("clear");
        }
    });
    productItem_datagrid.datagrid({
        //url:'/product/selectAllProduct.do',
        //fit:true,
        fitColumns: true,
        columns: [[
            {field: 'id', hidden: true},
            {field: 'productName', title: '保险产品', width: 100},
            {field: 'sn', title: '保险编号', width: 100},
            {field: 'annualFee', title: '保险年费', width: 100},
        ]],
    })
    audit_dialog.dialog({
        closed: true,
        title: '查看商品信息',
        width: 700,
        height: 600,
        buttons: '#dialog_buttons'
    })
    audit_datagrid.datagrid({
        url: "/temporary/queryAudit.do",
        fitColumns: true, // 自适应列
        fit: true,
        striped: true,  // 斑马线
        pagination: true,
        pageSize: 10,
        pageNumber: 1,
        singleSelect: true, // 单选
        toolbar: "#audit_toolbar",
        columns: [
            [
                {field: 'id', hidden: true},
                {field: 'sn', title: '保单编号', width: 80},
                {
                    field: 'client.id', hidden: true, formatter: function (value, row) {
                        if (row.client) {
                            return row.id
                        }
                    }
                },
                {
                    field: 'client', title: '投保人', width: 80, formatter: function (value, row) {
                        if (row.client) {
                            return row.client.name
                        }
                    }
                },
                {field: 'applyDate', title: '申请日期', width: 80},
                {
                    field: 'time', title: '保险时长', width: 80
                },
                {
                    field: 'inputUser', title: '业务员', width: 80, formatter: function (value, row) {
                        if (row.inputUser) {
                            return row.inputUser.username
                        }
                    }
                },
                {field: 'totalAmount', title: '投保总金额', width: 80},
                {
                    field: 'item', title: '查看明细', width: 80, formatter: function () {
                        return '<a class="showProduct">查看产品明细</a>';
                    }
                },
                {
                    field: 'clientInfo', title: '查看投保人', width: 80, formatter: function () {

                        return '<a class="showClient">查看投保人信息</a>';
                    }
                }
            ]]
    })
    $("#myDiv").on("click", "a.showProduct", function () {
        //获取当前选中的行
        var row = audit_datagrid.datagrid('getSelected');
        //回显
        $.get('/car/selectByInsureId.do', {insureId: row.id}, function (data) {
            $("input[tag='carOwnName']").val(data.owner)
            $("input[tag='carNum']").val(data.carNum)
            $("input[tag='buyDate']").val(data.purchaseDate)
            $("input[tag='brand']").val(data.brand)
            $("input[tag='model']").val(data.productName)
            $("input[tag='exhaust']").val(data.displacement)
            $("input[tag='price']").val(data.evaluation)
            $("input[tag='carClass']").val(data.type)
            $("input[tag='size']").val(data.size)
        })

        //回显保险时长
        $("input[tag='burningTime']").val(row.time)
        //回显总金额
        $("#totalAmount").html(row.totalAmount)
        //回显明细表格
        $.get('/product/selectAllProduct.do', {insureId: row.id}, function (data) {
            productItem_datagrid.datagrid('loadData', data);
            $("input[tag='organization']").val(data[0].organization.name)
        })
        audit_dialog.dialog('open')
    }).on("click", "a.showClient", function () {
        var row = audit_datagrid.datagrid('getSelected');
        $.get("/client/selectByPrimaryKey.do", {id: row.client.id},function (data) {
            client_form.form('load',data)
        })
        client_dialog.dialog('open')
    })
    var objMethod = {
        query: function () {
            //获取高级查询的参数值
            var clientName = $("#clientName").textbox('getValue')
            var sn = $("#sn").textbox('getValue')
            //重新加载数据表格
            audit_datagrid.datagrid('load', {
                clientName: clientName,
                sn: sn
            });
        },
        cancel: function () {
            audit_dialog.dialog('close')
        },
        refresh: function () {
            //刷新数据表格
            audit_datagrid.datagrid('reload')
        },
        auditSuccess: function () {
            var row = audit_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return
            }
            $.get('/temporary/changeState.do', {id: row.id, state: 4}, function (data) {
                if (data.success) {
                    $.messager.alert('友情提示', '提交成功,请及时提醒用户缴费', 'info', function () {
                        //刷新数据表格
                        audit_datagrid.datagrid('reload')
                    });
                } else {
                    $.messager.alert("友情提示", data.msg, "warning")
                }
            })
        },
        amend: function () {//退回申请
            var row = audit_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return
            }
            $.messager.prompt('提示信息', '请输入退改事由', function (r) {
                if (r) {
                    $.get('/temporary/amend.do', {id: row.id, state: 3, opinion: r}, function (data) {
                        if (data.success) {
                            $.messager.alert('友情提示', '已退回申请', 'info', function () {
                                //刷新数据表格
                                audit_datagrid.datagrid('reload')
                            });
                        } else {
                            $.messager.alert("友情提示", data.msg, "warning")
                        }
                    })
                }
            });
        },
        refuse: function () {//退回申请
            var row = audit_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return
            }
            $.messager.prompt('提示信息', '请输入拒保原因', function (r) {
                if (r) {
                    $.get('/temporary/amend.do', {id: row.id, state: 0, opinion: r}, function (data) {
                        if (data.success) {
                            $.messager.alert('友情提示', '拒保成功', 'info', function () {
                                //刷新数据表格
                                audit_datagrid.datagrid('reload')
                            });
                        } else {
                            $.messager.alert("友情提示", data.msg, "warning")
                        }
                    })
                }
            });
        }
    }
    // 绑定事件
    $('a[data-cmd]').click(function () {
        objMethod[$(this).data('cmd')]();
    })
})